package org.example.Dao;

import org.example.Entity.Dam;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DamDao {

    private Connection conn = null;
    private PreparedStatement pstmt = null;
    private ResultSet rs = null;

    public DamDao(Connection conn) {
        this.conn = conn;
    }

    // 添加水库
    public boolean addDam(Dam dam) {
        String sql = "INSERT INTO dam(name, river, capacity, completeDate) VALUES(?, ?, ?, ?)";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, dam.getName());
            pstmt.setString(2, dam.getRiver());
            pstmt.setDouble(3, dam.getCapacity());
            pstmt.setString(4, dam.getCompleteDate());
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return false;
    }

    // 更新水库
    public boolean updateDam(Dam dam) {
        String sql = "UPDATE dam SET name=?, river=?, capacity=?, completeDate=? WHERE id=?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, dam.getName());
            pstmt.setString(2, dam.getRiver());
            pstmt.setDouble(3, dam.getCapacity());
            pstmt.setString(4, dam.getCompleteDate());
            pstmt.setInt(5, dam.getId());
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return false;
    }

    // 删除水库
    public boolean deleteDam(int id) {
        String sql = "DELETE FROM dam WHERE id=?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id);
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return false;
    }

    // 获取所有水库
    public List<Dam> getAllDams() {
        List<Dam> dams = new ArrayList<>();
        String sql = "SELECT * FROM dam";
        try {
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                Dam dam = new Dam();
                dam.setId(rs.getInt("id"));
                dam.setName(rs.getString("name"));
                dam.setRiver(rs.getString("river"));
                dam.setCapacity(rs.getDouble("capacity"));
                dam.setCompleteDate(rs.getString("completeDate"));
                dams.add(dam);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return dams;
    }

    // 根据ID获取水库
    public Dam getDamById(int id) {
        Dam dam = null;
        String sql = "SELECT * FROM dam WHERE id=?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                dam = new Dam();
                dam.setId(rs.getInt("id"));
                dam.setName(rs.getString("name"));
                dam.setRiver(rs.getString("river"));
                dam.setCapacity(rs.getDouble("capacity"));
                dam.setCompleteDate(rs.getString("completeDate"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close();
        }

        return dam;
    }

    // 根据条件查询水库
    public List<Dam> searchDams(String name, String river) {
        List<Dam> dams = new ArrayList<>();
        String sql = "SELECT * FROM dam WHERE 1=1";
        List<Object> params = new ArrayList<>();

        if (name != null && !name.isEmpty()) {
            sql += " AND name LIKE ?";
            params.add("%" + name + "%");
        }

        if (river != null && !river.isEmpty()) {
            sql += " AND river LIKE ?";
            params.add("%" + river + "%");
        }

        try {
            pstmt = conn.prepareStatement(sql);
            for (int i = 0; i < params.size(); i++) {
                pstmt.setObject(i + 1, params.get(i));
            }
            rs = pstmt.executeQuery();
            while (rs.next()) {
                Dam dam = new Dam();
                dam.setId(rs.getInt("id"));
                dam.setName(rs.getString("name"));
                dam.setRiver(rs.getString("river"));
                dam.setCapacity(rs.getDouble("capacity"));
                dam.setCompleteDate(rs.getString("completeDate"));
                dams.add(dam);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close();
        }

        return dams;
    }

    private void close() {
        try {
            if (rs != null) rs.close();
            if (pstmt != null) pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}